<?php
  $key    = md5($_REQUEST['u']);
  $dbconn = pg_connect("host=localhost dbname=gc user=postgres");
  $result = pg_query("select xml from getcaps where key = '$key'");
  $xml    = '';
  while ($line = pg_fetch_array($result)) {
    $xml = $line[0];
  }
  if ($xml == '') {
    $xml = file_get_contents($_REQUEST['u']);
    pg_query(sprintf(
      "insert into getcaps(key,url,xml) values ('%s','%s','%s')"
      ,$key
      ,pg_escape_string($_REQUEST['u'])
      ,pg_escape_string($xml)
    ));
  }
  pg_close($dbconn);
  header("Content-Type:text/xml");
  $xml = preg_replace('/<Contents([^>]*>)/i','<sos:Contents xmlns:sos="http://www.opengis.net/sos/1.0">',$xml);
  $xml = preg_replace('/<\/Contents/i','</sos:Contents',$xml);
  echo $xml;
?>
